#include <QtTest/QtTest>
#include <QtHelp/QHelpEngineCore>

class QtQchReader_test: public QObject
{
    Q_OBJECT
private slots:
    void first();
    void setupData();
    void readDirQchFile();
    void registerDocumentation();
};

void QtQchReader_test::first()
{
    QCOMPARE(true, true);
}

void QtQchReader_test::setupData()
{
    QString collectionFile;
    QHelpEngineCore * helpEngine = new QHelpEngineCore(collectionFile, this);
    if (!helpEngine->setupData())
    {
        delete helpEngine;
        helpEngine = 0;
    }
    QCOMPARE(true, helpEngine != NULL);
}

void QtQchReader_test::readDirQchFile()
{
    QString dirPath = "D:\\Qt\\Qt5.10.1\\Docs\\Qt-5.10.1\\";
    QStringList filters;
    filters << "*.qch";
    QStringList files = QDir(dirPath).entryList(filters);

    QCOMPARE(true, files.size() > 0);
}

void QtQchReader_test::registerDocumentation()
{
    QString currentPath = QDir::currentPath();
    QString collectionFile = "../docs/wateringmachine.qhc";

    QHelpEngineCore * helpEngine = new QHelpEngineCore(collectionFile, this);
    if (!helpEngine->setupData())
    {
        delete helpEngine;
        helpEngine = 0;
    }
    QCOMPARE(true, helpEngine != NULL);

    if(helpEngine != NULL)
    {
        QString dirPath = "D:\\Qt\\Qt5.10.1\\Docs\\Qt-5.10.1\\";
        QStringList filters;
        filters << "*.qch";
        QStringList files = QDir(dirPath).entryList(filters);

        QCOMPARE(true, files.size() > 0);
        for(int i = 0; i < files.size(); i++)
        {
            QString QchFile = files[i];

            QString QchFileNameSpace;
            QchFileNameSpace = helpEngine->namespaceName(dirPath+QchFile);
            QString err;
            helpEngine->unregisterDocumentation(QchFileNameSpace);
            err = helpEngine->error();
            bool isRegist = helpEngine->registerDocumentation(dirPath+QchFile);
            err = helpEngine->error();
            if(isRegist)
            {
                QList<QUrl> urls = helpEngine->files(QchFileNameSpace, QStringList(), "html");

                QCOMPARE(true, urls.size() > 0);
                QFile file;
                file.setFileName(QchFileNameSpace + ".txt");

                if (!file.open(QIODevice::WriteOnly | QIODevice::Text))
                    return;

                for(int i = 0; i < urls.size(); i++)
                {
                    file.write(helpEngine->fileData(urls[i]));
                }
                file.close();
            }
        }
    }
}


QTEST_MAIN(QtQchReader_test)
#include "QtQchReader_test.moc"

